package top.whysu.edevice.controller.app;

import liquibase.pro.packaged.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.whysu.edevice.constant.AppConstant;
import top.whysu.edevice.constant.DataDictionary;
import top.whysu.edevice.dto.app.AppRequestDTO;
import top.whysu.edevice.dto.app.AppResponseDTO;
import top.whysu.edevice.dto.app.account.*;
import top.whysu.edevice.exception.AppException;
import top.whysu.edevice.service.app.AppService;
import top.whysu.edevice.utils.AppResponseUtil;

import javax.annotation.Resource;

/**
 * app 相关
 */
@Controller
@RequestMapping("/app")
public class AppController {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppController.class);

    @Resource
    private AppService appService;

    /**
     * 邀请码注册
     */
    @PostMapping("/register")
    @ResponseBody
    public AppResponseDTO register(@RequestBody AppRequestDTO<AppRegisterDTO> requestDTO) {
        try {
            appService.wholeAppRegister(requestDTO.getParams());
        } catch (AppException e) {
            return AppResponseUtil.getFailResponse(requestDTO, e.getResult(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("app注册账号系统异常", e);
            return AppResponseUtil.getFailResponse(requestDTO, AppConstant.Code.RESULT_CODE_ACCESS_TOKEN_EXPIRED, "系统异常");
        }
        return AppResponseUtil.getSuccessResponse(requestDTO);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    @ResponseBody
    public AppResponseDTO<AppLoginResponseDTO> login(@RequestBody AppRequestDTO<AppLoginRequestDTO> requestDTO) {
        try {
            AppLoginResponseDTO appLoginResponseDTO = appService.wholeLogin(requestDTO.getParams());
            return AppResponseUtil.getSuccessResponse(requestDTO, appLoginResponseDTO);
        } catch (AppException e) {
            return AppResponseUtil.getFailResponse(requestDTO, e.getResult(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("app登录系统异常", e);
            return AppResponseUtil.getFailResponse(requestDTO, AppConstant.Code.RESULT_CODE_ACCESS_TOKEN_EXPIRED, "系统异常");
        }
    }

    /**
     * 获取accessToken
     */
    @PostMapping("/getAccessToken")
    @ResponseBody
    public AppResponseDTO<GetAccessTokenResponseDTO> getAccessToken(@RequestBody AppRequestDTO<GetAccessTokenRequestDTO> requestDTO) {
        try {
            GetAccessTokenResponseDTO getAccessTokenResponseDTO = appService.getAccessToken(requestDTO.getParams().getUserName());
            return AppResponseUtil.getSuccessResponse(requestDTO, getAccessTokenResponseDTO);
        } catch (AppException e) {
            return AppResponseUtil.getFailResponse(requestDTO, e.getResult(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("app获取accessToken系统错误", e);
            return AppResponseUtil.getFailResponse(requestDTO, AppConstant.Code.RESULT_CODE_ACCESS_TOKEN_EXPIRED, "系统异常");
        }
    }
}
